﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Properties;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Plugins.ViewManagement;
using MethodWorx.CMS.Providers;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Page
{
    public class Repeater : Pages, IParser, IDesignable, IMetaParser
    {
        public Repeater()
        {
            this.Options = "DirectDescendants";
        }

    
        [Property(Description = "Set an arbitrary break index to allow template customization at the item index")]
        public int? BreakIndex { get; set; }

        [Property(Description = "Order the results")]
        public string OrderBy { get; set; }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            List<EnumerableResult> results = new List<EnumerableResult>();
            var currentPage = stack.OfType<MethodWorx.CMS.Content.Page>().FirstOrDefault();

            bool first = true;
            var pages = this.GetPages(context, currentPage).OrderBy(p => p.DateCreated).ToList();
            var i = 1;
            if (pages.Count == 0)
            {
                results.Add(new EnumerableResult()
                {
                    Names = new string [] { "empty" },
                    Result = new object()
                });
            }
            else
            {
                if (this.OrderBy == "DatePublishedDescending")
                    pages = pages.OrderByDescending(p => p.DatePublished).ToList();
                else if (this.OrderBy == "DatePublished")
                    pages = pages.OrderBy(p => p.DatePublished).ToList();

                foreach (var page in pages)
                {
                    List<string> names = new List<string>();
                    if (first)
                    {
                        names.Add("first");
                        first = false;
                    }

                    //
                    //  ok, this is where we add the item to the list
                    if (page == context.Page)
                    {
                        names.Add("current");
                    }

                    names.Add("default");

                    if (this.BreakIndex.HasValue)
                    {
                        //
                        //  we have a break, if we are at the position then
                        //  add the item in
                        if (i % this.BreakIndex.Value == 0)
                        {
                            names.Add("break");
                        }
                    }

                    results.Add(new EnumerableResult()
                    {
                        Names = names.ToArray(),
                        Result = page
                    });



                    i++;
                }
            }

            return results;
        }

        public string Name
        {
            get { return "Repeater";  }
        }

        public string Namespace
        {
            get { return "Page"; }
        }

        public void Design(IDesigner designer)
        {
            var control = designer.AddControl("Page:Repeater");
            control.AddChildControl("current")
                   .AddChildControl("first")
                   .AddChildControl("default")
                   .AddChildControl("notexists");


        }

        public string GetDescription()
        {
            return "Enumerates all matching pages from a set of filters";
        }

        public ParserMetaParameter[] GetParameters(Stack<object> stack, ParserMetaParameter[] parameters)
        {
            return parameters;
        }
    }
}
